import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STComponent, STColumn, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { FreightAccountService } from '../../../services/freight-account.service';

@Component({
  selector: 'app-advance-collection-detail',
  templateUrl: './advance-collection-detail.component.html',
  styleUrls: ['../../../../commom/less/box.less', '../../../../commom/less/basic-board.less']
})
export class AdvanceCollectionDetailComponent implements OnInit {
  @ViewChild('inputST', { static: true })
  inputST!: STComponent;
  columns: { [key: string]: STColumn[] } = this.initST();
  @ViewChild('infoST', { static: true })
  infoST!: STComponent;
  costInfo: any = {};
  id: any = null;

  @ViewChild('sf', { static: false })
  inputSF!: SFComponent;
  @ViewChild('sf', { static: false })
  infoSF!: SFComponent;
  inputSearchSchema: SFSchema = this.initInputSF();
  infoSearchSchema: SFSchema = this.initInfoSF();
  constructor(public service: FreightAccountService, private route: ActivatedRoute) {
    this.id = route.snapshot.params.id;
    this.loadDetail(this.id);
  }

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, {
      id: this.id
    });
    return requestOptions;
  };

  loadDetail(id: any) {
    this.service.request(this.service.$api_get_advance_collection_header, { id }).subscribe(res => {
      if (res) {
        this.costInfo = res;
      }
    });
  }

  /**
   * 重置表单
   */
  resetInputSF() {
    this.inputSF.reset();
  }
  /**
   * 重置表单
   */
  resetInfoSF() {
    this.infoSF.reset();
  }

  goBack() {
    history.go(-1);
  }

  private initInputSF(): SFSchema {
    return {
      properties: {
        feedate: {
          title: '到账日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        billHCode: {
          type: 'string',
          title: '收款单号',
          ui: {
            placeholder: '请输入'
          }
        }
      }
    };
  }

  private initInfoSF(): SFSchema {
    return {
      properties: {
        feedate: {
          title: '核销日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        billHCode: {
          type: 'string',
          title: '核销单号',
          ui: {
            placeholder: '请输入'
          }
        }
      }
    };
  }

  private initST(): { [key: string]: STColumn[] } {
    return {
      input: [
        { title: '序号', render: 'no', width: 70, className: 'text-left' },
        { title: '收款单号', index: 'brmHCode', className: 'text-left', width: 200 },
        // { title: '到账日期', index: 'billHId', className: 'text-center', width: 150 },
        {
          title: '到账金额',
          index: 'yskmoney',
          type: 'widget',
          className: 'text-right',
          widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yskmoney }) },
          width: 140
        },
        {
          title: '转预收金额',
          index: 'yskmoney',
          type: 'widget',
          className: 'text-right',
          widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yskmoney }) },
          width: 140
        },
        // { title: '预收备注', index: 'callNo', width: 200 }
      ],
      info: [
        { title: '序号', render: 'no', width: 70, className: 'text-left' },
        { title: '核销单号', index: 'ahxcode', className: 'text-left', width: 200 },
        { title: '核销日期', index: 'ahxdate', className: 'text-center', width: 150 },
        {
          title: '核销金额',
          index: 'ahxmoney',
          type: 'widget',
          className: 'text-right',
          widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.ahxmoney }) },
          width: 200
        },
        { title: '核销备注', index: 'remarks', className: 'text-left', width: 200 }
      ]
    };
  }
}
